#include <huniplacer_3d/Indicator.h>
#include <math.h>

#include <GL/gl.h>
#include <GL/freeglut.h>
#include <iostream>
#include <stdio.h>
#include <boost/lexical_cast.hpp>
#include<cstring>

#include <huniplacer_3d/PanelPart.h>

Indicator::Indicator(unsigned int x, unsigned int y, unsigned int radius, float r, float g, float b, float bg_r, float bg_g, float bg_b, double value, double min_value, double max_value, char* value_type, char* name) :
				PanelPart(name, x, y, radius, radius, r, g, b, bg_r, bg_g, bg_b, value_type) {
	current_value = value;
	this->min_value = min_value;
	this->max_value = max_value;
}

void Indicator::draw() {
	int screen_height = glutGet(GLUT_SCREEN_HEIGHT);

	float t, dt;

	int n = 30;
	int i;

	if (bg_color_r >= 0) {
		/* radians */
		t = 180 * M_PI/(180);
		dt = 180 * M_PI/(180*n); /* increment */
	
		set_color3f(bg_color_r, bg_color_g, bg_color_b);
		//fill background of the indicator
		glBegin(GL_POLYGON);
		for(i=0; i<=n; i++, t += dt)
			glVertex2f((x) + (width+10)*cos(t), (screen_height - (y)) + (height + 10)*sin(t));
		glEnd();
	}
	
	/* radians */
	t = 180 * M_PI/180;
	dt = 180 * M_PI/(180*n); /* increment */

	//draw the indicator itself
	set_color3f(color_r, color_g, color_b);
	glBegin(GL_LINE_STRIP);
	for(i=0; i<=n; i++, t += dt)
		glVertex2f(x + width*cos(t), (screen_height - y) + height*sin(t));
	glEnd();

	double arrow_degrees = (current_value-min_value)/(max_value-min_value)*M_PI;
	
	glBegin(GL_LINES);
		glVertex2f(x, screen_height - y); glVertex2f(x - width*cos(arrow_degrees)*0.9, screen_height - y - height*sin(arrow_degrees)*0.9);
	glEnd();
   
   
   
	std::ostringstream progressInfo;
	progressInfo 	<< initial_name
						<< ": "
						<< current_value
						<< value_type;
	
	name = progressInfo.str();
}
